
package jh.com.jhappbf.weight.query;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.ab.activity.AbActivity;
import com.ab.fragment.AbDialogFragment;
import com.ab.fragment.AbLoadDialogFragment;
import com.ab.util.AbDialogUtil;
import com.ab.view.titlebar.AbTitleBar;
import com.android.volley.Request.Priority;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;

import org.json.JSONObject;

import java.util.ArrayList;

import androidx.core.content.ContextCompat;
import jh.com.jhappbf.R;
import jh.com.jhappbf.database.SQLiteDataBaseUtil;
import jh.com.jhappbf.httputils.VolleyCallWebService;
import jh.com.jhappbf.httputils.VolleySingleton;
import jh.com.jhappbf.utils.JsonHelper;

/**
 * @author Administrator
 */
public class QueryWeightDetail extends AbActivity {
    private TextView mTxt;
    private Context mAppContext;
    private AbLoadDialogFragment mDialogFragment = null;
    private Button mTitleRightBtn;
    private String mId; // 过磅编号


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setAbContentView(R.layout.query_weight_detail_activity);

        AbTitleBar mAbTitleBar = this.getTitleBar();
        mAbTitleBar.setTitleText(R.string.query_weight_detail_title);
        mAbTitleBar.setLogo(R.drawable.button_selector_back);
        mAbTitleBar.setTitleBarBackgroundColor(ContextCompat.getColor(this, R.color.orange));
        mAbTitleBar.setTitleTextMargin(10, 0, 0, 0);
        mAbTitleBar.setLogoLine(R.mipmap.line);
        View rightViewMore = mInflater.inflate(R.layout.title_right_btn, null);
        mAbTitleBar.addRightView(rightViewMore);
        mTitleRightBtn = rightViewMore.findViewById(R.id.title_right_btn);
        initView();
    }

    private void initView() {

        mAppContext = getApplicationContext();
        mTxt = findViewById(R.id.query_weight_detail_txt);
        mTitleRightBtn.setOnClickListener(view -> {
            Intent imgIntent = new Intent(QueryWeightDetail.this, QueryWeightImage.class);
            imgIntent.putExtra("flag", mId);
            startActivity(imgIntent);
        });

        Intent i = getIntent();
        mId = i.getStringExtra("flag");
        loadFirst();
    }


    /**
     * 第一次加载
     */
    private void loadFirst() {

        mDialogFragment = AbDialogUtil.showLoadDialog(this, R.mipmap.ic_load,
                "查询中,请稍等...");
        mDialogFragment.setCancelable(false);
        mDialogFragment.setAbDialogOnLoadListener(() -> getData());
    }

    /**
     * 获取数据
     *
     * @param
     * @param
     */
    private void getData() {

        String fields = getFields();
        String methodName = "queryWeightDetail";
        ArrayList<String> param = new ArrayList<>();
        ArrayList<String> value = new ArrayList<>();
        param.add("id");
        param.add("fileds");
        value.add(mId);
        value.add(fields);
        Listener<String> listener = response -> {
            AbDialogUtil.removeDialog(QueryWeightDetail.this);
            JSONObject obj = JsonHelper.getJsonObject(response);
            if (obj != null) {
                String result = obj.optString("result");
                mTxt.setText(result);
            }
        };

        ErrorListener errorListener = error -> AbDialogUtil.removeDialog(QueryWeightDetail.this);

        VolleyCallWebService webService = new VolleyCallWebService(methodName, param, value, listener, errorListener, Priority.NORMAL);
        VolleySingleton.getVolleyInstance(mAppContext).addRequest(webService);


    }

    @Override
    protected void onStop() {
        super.onStop();

    }

    /**
     * 返回需要显示的字符串
     *
     * @return
     */
    private String getFields() {
        Cursor cursor = null;
        String fields = "";
        try {
            SQLiteDatabase db = SQLiteDataBaseUtil.getDatabase(mAppContext);
            cursor = db.query("saverFileds", new String[]{"field"}, null,
                    null, null, null, " id asc ");
            if (cursor != null) {
                StringBuilder builder = new StringBuilder();
                String fieldString;
                while (cursor.moveToNext()) {
                    fieldString = cursor.getString(0);
                    if (!TextUtils.isEmpty(fieldString)) {
                        builder.append(fieldString);
                        builder.append(",");
                    }
                }
                if (builder.lastIndexOf(",") == builder.length() - 1) {
                    builder.deleteCharAt(builder.length() - 1);
                }
                fields = builder.toString();
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        return fields;
    }

    @Override
    public void onBackPressed() {
        finish();
    }
}